<?php

namespace app\selladmin\model;

use think\Model;
use think\Db;

/*
 * 店铺
 */

class Store extends Model {
    //店铺状态
    const IS_DEL_1 = 1; //正常
    const IS_DEL_2 = 2; //删除

    //营业执照状态
    const BUS_LICENCE_1 = 1; //有
    const BUS_LICENCE_2 = 2; //无

    //审核状态
    const AUDITSTORE_0 = 0; //未审核
    const AUDITSTORE_1 = 1; //审核通过
    const AUDITSTORE_2 = 2; //审核不通过

    //是否在小程序显示
    const IN_WXAPP_0 = 0; //不显示
    const IN_WXAPP_1 = 1; //显示

    //默认头像
    const DEFAULT_PHOTO = 'photo/default.jpg';

    //一周的全部天
    public static $weekArray = [1, 2, 3, 4, 5, 6, 7];

    //店铺对应的地区 
    public function ProvCityArea() {
        return $this->belongsTo('ProvCityArea', 'prov', 'id')->field('id,name');
    }

    //查询店铺信息
    public function StoreInfo($id) {
        //查询店铺信息
        $where['id'] = $id;
        $storeList = Db::name('store')->where($where)
                ->field("id,store_name,store_keeper,store_address,keeper_phone,add_time,busstarttime,busendtime,introduce,bus_licence_type,bus_licence_number,idcard1,idcard2,idcard3,bus_licence_img,img,auditstore,prov,city,area,weekday,keeper_idcard,bus_licence_name,why,store_img,store_phone,longitude,latitude,bus_licence,is_shareshop")
                ->find();
        if ($storeList['prov']) {
            $storeList['provname'] = Db::name('prov_city_area')->where('id=' . $storeList['prov'])->field('name')->find()['name'];
        }
        if ($storeList['city']) {
            $storeList['cityname'] = Db::name('prov_city_area')->where('id=' . $storeList['city'])->field('name')->find()['name'];
        }
        if ($storeList['area']) {
            $storeList['areaname'] = Db::name('prov_city_area')->where('id=' . $storeList['area'])->field('name')->find()['name'];
        }


        return $storeList;
    }
    
    /*
     * 根据店铺默认项目的数量更新该店铺是否在小程序显示
     */
    public static function updateInWxapp($store_id)
    {
        $in_wxapp = Db::name('store')->where('id', '=', $store_id)->value('in_wxapp');

        $count = Db::name('item')->where('store_id', '=', $store_id)
            ->where('type', '=', Item::TYPE_0)
            ->where('is_del', '=', Item::IS_DEL_1)
            ->count();

        if ($in_wxapp == self::IN_WXAPP_0) {
            if ($count >= 3) {
                Db::name('store')->where('id', '=', $store_id)->update(['in_wxapp' => self::IN_WXAPP_1]);
            }
        } else if ($in_wxapp == self::IN_WXAPP_1) {
            if ($count < 3) {
                Db::name('store')->where('id', '=', $store_id)->update(['in_wxapp' => self::IN_WXAPP_0]);
            }
        }

    }
    
    //店铺对应的销售
    public function Selladmin() {
        return $this->belongsTo('Selladmin', 'sell_id', 'id')->field('id,admin_note');
    }

}
